package com.blog.Initializer;

import com.blog.entity.Article;
import com.blog.mapper.ArticleMapper;
import com.blog.service.AsyncArticleSyncService;
import com.blog.service.KafkaProducerService;
import com.blog.utils.ArticleSearchUtils;
import com.blog.utils.IndexMappingCreator;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

@Component
@Slf4j
public class EsDataInitializer implements ApplicationRunner {
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private ArticleSearchUtils articleSearchUtils;
    @Resource
    private KafkaProducerService kafkaProducerService;
    @Resource
    private IndexMappingCreator indexMappingCreator;
    private static final int BATCH_SIZE = 10;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 创建索引(如果有索引就不做任何操作)
        indexMappingCreator.createBlogIndex();
        // 如果es中有数据
//        if(!articleSearchUtils.isIndexEmpty()) {
//            log.info("es中已有数据，跳过初始化");
//            return;
//        }
//        log.info("开始初始化es数据");
//        int pageNum = 1;
//        while(true) {
//            PageHelper.startPage(pageNum, BATCH_SIZE);
//            List<Article> batch = articleMapper.getAllArticles();
//            if(batch.isEmpty()) {
//                log.info("初始化完成,共同步{}批",pageNum - 1);
//                break;
//            }
//            kafkaProducerService.sendArticleBatch(batch,pageNum);
//            pageNum++;
//        }
    }
}